package com.spark.gaea.mvc.utils;

import com.spark.gaea.base.Page;
import com.spark.gaea.utils.GetterUtil;
import com.spark.gaea.utils.ValidateUtil;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ParamUtil {
	public static String getCookie(final HttpServletRequest request, final String key) {
		final Cookie[] cookies = request.getCookies();
		if (cookies != null && cookies.length > 0) {
			for (final Cookie cookie : cookies) {
				if (cookie.getName().equals(key)) {
					return cookie.getValue();
				}
			}
		}
		return null;
	}

	public static Date get(final HttpServletRequest request, String year, String month, String day,
			final Date defaultValue) {
		year = get(request, year, "");
		month = get(request, month, "");
		day = get(request, day, "");
		final StringBuilder format = new StringBuilder();
		if (ValidateUtil.isNotNull(year)) {
			format.append("yyyy");
		}
		if (ValidateUtil.isNotNull(month)) {
			format.append("MM");
		}
		if (ValidateUtil.isNotNull(day)) {
			format.append("dd");
		}
		if (format.toString().equals("")) {
			return defaultValue;
		}
		final SimpleDateFormat formater = new SimpleDateFormat(format.toString());
		try {
			return formater.parse(year + month + day);
		} catch (ParseException e) {
			return defaultValue;
		}
	}

	public static boolean get(final HttpServletRequest request, final String param, final boolean defaultValue) {
		return GetterUtil.get(request.getParameter(param), defaultValue);
	}

	public static double get(final HttpServletRequest request, final String param, final double defaultValue) {
		return GetterUtil.get(request.getParameter(param), defaultValue);
	}

	public static float get(final HttpServletRequest request, final String param, final float defaultValue) {
		return GetterUtil.get(request.getParameter(param), defaultValue);
	}

	public static int get(final HttpServletRequest request, final String param, final int defaultValue) {
		return GetterUtil.get(request.getParameter(param), defaultValue);
	}

	public static long get(final HttpServletRequest request, final String param, final long defaultValue) {
		return GetterUtil.get(request.getParameter(param), defaultValue);
	}

	public static short get(final HttpServletRequest request, final String param, final short defaultValue) {
		return GetterUtil.get(request.getParameter(param), defaultValue);
	}

	public static String get(final HttpServletRequest request, final String param, final String defaultValue) {
		if (param == null) {
			return defaultValue;
		}
		return GetterUtil.get(request.getParameter(param), defaultValue);
	}

	public static String[] gets(final HttpServletRequest request, final String param, final String[] defaultValue) {
		final String[] returnValues = request.getParameterValues(param);
		if (returnValues == null) {
			return defaultValue;
		}
		return returnValues;
	}

	public static String[] get(final HttpServletRequest request, final String param, final String splitor,
			final String[] defaultValue) {
		if (param == null) {
			return defaultValue;
		}
		final String noneSplit = get(request, param, null);
		if (noneSplit == null) {
			return defaultValue;
		}
		final String[] returnValues = noneSplit.split(splitor, -1);
		if (returnValues.length == 0) {
			return defaultValue;
		}
		return returnValues;
	}

	public static Page getPager(final HttpServletRequest request) {
		final Page page = new Page();
		page.setPage(GetterUtil.get(request.getParameter("page"), 1));
		page.setPagesize(GetterUtil.get(request.getParameter("limit"), 20));
		return page;
	}
}
